package com.hjm.project.ShopOrder.ShopOrder.utils;


import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

/**
 * 发送http请求
 * @author maker
 *
 */
public class HttpUtils {
	
	
		public static String doGet(String url,String appCode) {
			String result = "";
			BufferedReader in = null;
			try {
				URL realUrl = new URL(url);
				// 打开和URL之间的连接
				URLConnection connection = realUrl.openConnection();
				// 设置通用的请求属性
				connection.setRequestProperty("accept", "*/*");
				connection.setRequestProperty("connection", "Keep-Alive");
//				connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
			
				// 建立实际的连接
				connection.connect();
				// 获取所有响应头字段
				Map<String, List<String>> map = connection.getHeaderFields();
				// 遍历所有的响应头字段
				for (String key : map.keySet()) {
					System.out.println(key + "--->" + map.get(key));
				}
				// 定义 BufferedReader输入流来读取URL的响应
				in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
				String line;
				while ((line = in.readLine()) != null) {
					result += line;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			// 使用finally块来关闭输入流
			finally {
				try {
					if (in != null) {
						in.close();
					}
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
			return result;
		}
		
	
		
	
	
	
	public static String doPost(String urls,String param){
		BufferedReader rd =null;
		PrintStream out =null;
		StringBuffer sb = null;
		try {
			URL url = new URL(urls);
			HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
			urlCon.setRequestMethod("POST");
			urlCon.setRequestProperty("Content-length",String.valueOf(param.length()));
//			urlCon.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); 
			urlCon.setDoInput(true);
			urlCon.setDoOutput(true);
			urlCon.setUseCaches(false);
			urlCon.setConnectTimeout(120000);
			urlCon.setReadTimeout(120000);
			out = new PrintStream(urlCon.getOutputStream(), false);
//			out = new PrintStream(urlCon.getOutputStream(), false,"UTF-8");
			
			out.print(param);
		
			out.flush();
			
			
			rd = new BufferedReader(new InputStreamReader(urlCon.getInputStream(),"UTF-8"));  
			sb = new StringBuffer();
			String inputLine;
			while ((inputLine = rd.readLine()) != null){
				sb.append(inputLine);
			}
						
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
		try {
			if (rd != null) {
					rd.close();
			}
			if (out != null) {
				out.close();
			}
			} catch (IOException e) {
				
			}	
			}
		return sb.toString();
	}
	
	
	public static String doPostJson(String urls,String param){
		BufferedReader rd =null;
		PrintStream out =null;
		StringBuffer sb = null;
		try {
			URL url = new URL(urls);
			HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
			urlCon.setRequestMethod("POST");
			urlCon.setRequestProperty("Content-Type", "application/json");
			urlCon.setRequestProperty("Content-length",String.valueOf(param.length()));
//			urlCon.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); 
			urlCon.setDoInput(true);
			urlCon.setDoOutput(true);
			urlCon.setUseCaches(false);
			urlCon.setConnectTimeout(120000);
			urlCon.setReadTimeout(120000);
			out = new PrintStream(urlCon.getOutputStream(), false);
//			out = new PrintStream(urlCon.getOutputStream(), false,"UTF-8");
			
			out.print(param);
		
			out.flush();
			
			
			rd = new BufferedReader(new InputStreamReader(urlCon.getInputStream(),"UTF-8"));  
			sb = new StringBuffer();
			String inputLine;
			while ((inputLine = rd.readLine()) != null){
				sb.append(inputLine);
			}
						
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
		try {
			if (rd != null) {
					rd.close();
			}
			if (out != null) {
				out.close();
			}
			} catch (IOException e) {
				
			}	
			}
		return sb.toString();
	}
	
	

	
	//excel导出响应页面请求
    public static void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    

	

}
